#define _CRT_SECURE_NO_WARNINGS 1
#include"main.h"

LTNode* ListInit()
{
	LTNode* phead = (LTNode*)malloc(sizeof(LTNode*));
	assert(phead);
	phead->prev = phead;
	phead->next = phead;
	return phead;
}

void ListPrint(LTNode* phead)
{
	LTNode* cur = phead;
	printf("head->");
	while (cur->next != phead)
	{
		cur = cur->next;
		printf("%d->", cur->data);
	}
	printf("head\n");
}

LTNode* BuyListNode(LTDateType x)
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	assert(newnode);
	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	return newnode;
}

void ListPushBack(LTNode* phead, LTDateType x)
{
	/*LTNode* newnode = BuyListNode(x);
	LTNode* tail = phead;
	while (tail->next != phead)
	{
		tail = tail->next;
	}
	tail->next = newnode;
	newnode->next = phead;
	newnode->prev = tail;
	phead->prev = newnode;*/
	ListInsert(phead, x);
}

void ListPopBack(LTNode* phead)
{
	assert(phead->next != phead);
	/*LTNode* tail = phead;
	while (tail->next != phead)
	{
		tail = tail->next;
	}
	LTNode* prevTail = tail->prev;
	prevTail->next = phead;
	phead->prev = prevTail;
	free(tail);*/
	ListErase(phead->prev);
}

void ListPushFront(LTNode* phead, LTDateType x)
{
	/*LTNode* newnode = BuyListNode(x);
	LTNode* nextPhead = phead->next;
	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = nextPhead;
	nextPhead->prev = newnode;*/
	ListInsert(phead->next, x);
}

void ListPopFront(LTNode* phead)
{
	assert(phead->next != phead);
	/*LTNode* next = phead->next;
	LTNode* nextNext = next->next;
	phead->next = nextNext;
	nextNext->prev = phead;
	free(next);*/
	ListErase(phead->next);
}

LTNode* ListFind(LTNode* phead, LTDateType x)
{
	LTNode* cur = phead;
	while (cur->data != x)
	{
		cur = cur->next;
		if (cur->next == phead)
			return NULL;
	}
	return cur;

}
void ListInsert(LTNode* pos, LTDateType x)
{
	assert(pos);
	LTNode* newnode = BuyListNode(x);
	LTNode* prev = pos->prev;
	prev->next = newnode;
	newnode->prev = prev;
	newnode->next = pos;
	pos->prev = newnode;
}
void ListErase(LTNode* pos)
{
	assert(pos);
	LTNode* prev = pos->prev;
	LTNode* next = pos->next;
	next->prev = prev;
	prev->next = next;
}
